const http = require("http");
const fs = require("fs");
const path = require("path");
let filePath = '';
let fileName = process.argv[2];
if (!fileName) {
    filePath = path.join(__dirname, 'fileReader2.js');
} else {
    filePath = path.join(__dirname, fileName);
}
http.createServer((req, res) => {
    // 打开文件
    fs.open(filePath, 'r', function (err, fd) {
        if (err) {
            throw err;
        }
        console.log('open file success.');
        var buffer = Buffer.alloc(1024);
        // 读取文件
        fs.read(fd, buffer, 0, 1024, 0, function (err, bytesRead, buffer) {
            if (err) {
                throw err;
            }
            // 打印出buffer中存入的数据
            // console.log(buffer.toString());
            res.end(buffer.toString());
            // 关闭文件
            fs.close(fd, function () {
                console.log("操作完毕，关闭文件")
            });
        });
    });



}).listen(8081);